<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\api\controller\Importdata;
class Zipqueue extends Controller
{
    public function getZipqueList()
    {

        $data = request()->put();
        $length = $data['limit'];
        
        if (empty($data['page'])) {
            $page = 0;
        } else {
            $page = $data['page'] - 1;
        }
        $offset = $page * $length;
        $map = array();

        // 添加查询条件
        


        if ($data['status'] == 1) {
            $map['status'] = array("=", $data['status']);
        }

        if ($data['status'] == "0") {
            $map['status'] = array("=", $data['status']);
        }

        if ($data['status'] == "2") {
            $map['status'] = array("=", $data['status']);
        }
        if($data['status'] == "3"){
            $map['status'] = array("=", $data['status']);
        }


        if ($data['task_id'] != "") {
            $map['task_id'] = array("=", $data['task_id']);
        }


        // 只查询未删除的记录
        $map['is_delete'] = array("=", 0);

        // 查询数据
        $result = Db::table('zstp_zip_queue')
            ->field("*")
            ->where($map)
            ->limit($offset, $length)
            ->order(array("id" => "desc"))
            ->select();

        // 获取总数
        $totalCount = Db::table('zstp_zip_queue')
            ->where($map)
            ->count();

        return json(array(
            "code" => 200,
            "data" => array("resultList" => $result),
            "totalCount" => $totalCount,
        ));
        
    }


    public function zipqueueApi(){
        
        $data = request()->post();
        $id = isset($data['id']) ? intval($data['id']) : 0;
        if($id == 0){
            return json([
                'code' => 400,
                'message' => '参数错误'
            ]);
        }

        $map['id'] = $id;
        $map['is_delete'] = 0;
        $queue = Db::table('zstp_zip_queue')->where($map)->find();
        
        if($queue){
            // 查任务
            $task = Db::table('zstp_zip_task')->where('id', $queue['task_id'])->find();   
        }

        if($task && $queue){

            $importdata = new Importdata();

            $zipQueueList=[$queue ];

           $res= $importdata->processQueues($task, $zipQueueList);

           return json($res);


           
        }else{
            return json([
                'code' => 400,
                'message' => '任务或队列不存在'
            ]);
        }




    }


}